﻿@{
    ViewBag.Title = "出库单";
    Layout = "~/Areas/IM/Views/Shared/_Layout.cshtml";
}
@section Scripts{
<script src="~/Scripts/pinyin.js"></script>
    <script type="text/javascript">
        //new 数组
        var Materials = new Array();
        //页面加载
        $(document).ready(function () {
            DisableBackspace();
            $.InitDateTime();
            initSearch();
            var urlstr = '@Url.Content("~/IM/AllDictionary/GetMSDictionaryInfo?TypeCode=MatertalOutType")';
            $.EUIcombobox("#DeliveryType", {
                url: urlstr,
                valueField: 'ID',
                textField: 'Name',
                editable: false,
                panelHeight: 'auto',
                OneOption: [{
                    ID: "",
                    Name: "--请选择--"
                }]
            });
            var urlstr1 = '@Url.Content("~/IM/AllDictionary/GetStorageCombo")';
            $.EUIcombobox("#SelectStorage", {
                url: urlstr1,
                valueField: 'ID',
                textField: 'Name',
                editable: false,
                panelHeight: 'auto',
                datainfo: {},
                OneOption: [{
                    ID: "",
                    Name: "--请选择--"
                }]
            });
            var urlstr2 = '@Url.Content("~/IM/AllDictionary/GetAllStorage")';
            $.EUIcomboboxTree("#TargetStorage", {
                url: urlstr2,//ajax后台取数据路径，返回的是json格式的数据
                OneOption: [{
                    id: "",
                    text: "--请选择--",
                }],
                onSelect: function (node) {
                    var t = $('#TargetStorage').combotree('tree');
                    var listnode = t.tree('getChildren', node.target);
                    if (listnode == null) {
                        $('#TStorageList').val(node.id);
                    } else {
                        var tSList = node.id;
                        for (var i = 0; i < listnode.length; i++) {
                            tSList = tSList + "," + listnode[i].id
                        }
                        $('#TStorageList').val(tSList);
                    }
                }
            });

            $('#txtIFCheck').combobox({
                valueField: 'ID',
                textField: 'Name',
                method: 'get',
                editable: false,
                panelHeight: 'auto',
                data: [
                {
                    ID: '',
                    Name: '全部'
                },
                {
                    ID: "true",
                    Name: '是'
                },
                {
                    ID: "false",
                    Name: '否'
                }
                ]
            });


            //绑定问所有查询条件，执行查询
            bindGrid();
            if ('@ViewData["RolePermissionInsert"]' == "Insert") {
                $('div.datagrid-toolbar div').eq(0).show();
                $('div.datagrid-toolbar a').eq(0).show();               
            } else if ('@ViewData["RolePermissionInsert"]' == "") {
                $('div.datagrid-toolbar div').eq(0).hide();
                $('div.datagrid-toolbar a').eq(0).hide();
            }
            $.setGridWithSearchBar("#TableForm", "#SearchBar", "#gridDeliveryList");

        });
        //页面动态改动
        $(window).resize(function () {
            $.setGridWithSearchBar("#TableForm", "#SearchBar", "#gridDeliveryList");
        });
        //重新查询
        function reSearch() {
            $("#gridDeliveryList").datagrid({
                pageNumber: 1,//20151105增加点击查询按钮返回第一页
                queryParams: {
                    StartDate: $('#StartDate').datebox('getValue'),
                    EndDate: $('#EndDate').datebox('getValue'),
                    DeliveryCode: $('#DeliveryCode').textbox('getValue'),
                    SelectStorage: $('#SelectStorage').combobox('getValue'),
                    TargetStorage: $('#TStorageList').val(),
                    DeliveryType: $('#DeliveryType').combobox('getValue'),
                    MaterialType: $("#MaterialTypeInfo").val(),
                    MaterialID: $("#MaterialID").val(),
                    IFCheck: $('#txtIFCheck').combobox('getValue')
                }
            });
            if ('@ViewData["RolePermissionInsert"]' == "Insert") {
                $('div.datagrid-toolbar div').eq(0).show();
                $('div.datagrid-toolbar a').eq(0).show();
            } else if ('@ViewData["RolePermissionInsert"]' == "") {
                $('div.datagrid-toolbar div').eq(0).hide();
                $('div.datagrid-toolbar a').eq(0).hide();
            }
        }

        function bindGrid() {
            $("#gridDeliveryList").datagrid({
                width: 'auto',
                height: 'auto',
                fitColumns: true,
                striped: true,
                singleSelect: false,
                url: '@Url.Content("~/IM/I_Delivery/DataLoad")',
                loadMsg: '数据加载中请稍后……',
                pagination: true,//启用分页，默认每页10行
                rownumbers: true,//显示页码，默认 提供 10 - 50 的页容量选择下拉框
                pageSize: 15,//设置 页容量5为
                pageList: [10, 15, 20],//设置 页容量下拉框
                queryParams: {
                    StartDate: $('#StartDate').datebox('getValue'),
                    EndDate: $('#EndDate').datebox('getValue'),
                    DeliveryCode: $('#DeliveryCode').textbox('getValue'),
                    SelectStorage: $('#SelectStorage').combobox('getValue'),
                    TargetStorage: $('#TStorageList').val(),
                    DeliveryType: $('#DeliveryType').combobox('getValue'),
                    MaterialType: $("#MaterialTypeInfo").val(),
                    MaterialID: $("#MaterialID").val(),
                    IFCheck: $('#txtIFCheck').combobox('getValue')
                },
                //{"MaterialMTypeID":"MaterialType-16","MaterialName":"硝酸甘油针","MaterialManufacturer":"","MaterialVendor":"","MaterialAlarmCounts":0,"MaterialSpecification":"5mg","MaterialRealPrice":4.0000,"MaterialUnit":"MatertalUnit-7","MaterialUnitName":"支","EntryStorageName":"装备科仓库","TargetStorageName":"东区仓库","OperatorName":"管理员","ConsigneeID":"4","Remark":"","DeliveryType":"MatertalOutType-1","DeliveryTypeName":"直接出库单","DeliveryDetailCode":"2015051417145041","DeliveryCode":"201505141714504","RealBatchNo":"20190920","BatchNo":"24520161129","DeliveryTime":"\/Date(1431594889200)\/","OperatorCode":4,"EntryStorageCode":"00002","TargetStorageCode":"10001","TargetEntryDetailCode":"2015051417145041","DetailRemark":"","RedDeliveryDetailCode":null,"MaterialID":245}
                columns: [[
                   { field: 'ck', checkbox: true },
                   { title: '出库日期', field: 'DeliveryTime', width: '12%', sortable: false, formatter: $.formatTime },
                   { title: '批次号', field: 'RealBatchNo', width: '8%', sortable: false, formatter: $.formatRealBatchNo },
                   { title: '物资代码', field: 'MCode', width: '7%', sortable: false },
                   { title: '物资名称', field: 'MaterialName', width: '10%', sortable: false },
                   { title: '出库数量', field: 'DeliveryCounts', width: '5%', sortable: false },
                   { title: '规格', field: 'MaterialSpecification', width: '5%', sortable: false },
                   { title: '单位', field: 'MaterialUnitName', width: '5%', sortable: false },
                   { title: '源仓库', field: 'EntryStorageName', width: '10%', sortable: false },
                   { title: '目标仓库', field: 'TargetStorageName', width: '10%', sortable: false },
                   { title: '经办人', field: 'OperatorName', width: '7%', sortable: false },
                   { title: '出库单类型', field: 'DeliveryTypeName', width: '8%', sortable: false },
                     {
                         title: '是否入账', field: 'IFCheck', width: '6%', sortable: false,
                         formatter: function (value, rowData, rowIndex) {
                             if (value == true) {
                                 return "是";
                             }
                             else {
                                 return "否";
                             }
                         }
                     },
                      { title: '入账时间', field: 'CheckTime', width: '10%', sortable: false, formatter: $.formatTime },
                   { title: '备注', field: 'DetailRemark', width: '15%', sortable: false },
                   { title: 'EntryType', field: 'EntryType', hidden: true },
                   { title: 'MaterialID', field: 'MaterialID', hidden: true },
                     { title: 'DeliveryDetailCode', field: 'DeliveryDetailCode', hidden: true }

                ]],
                rowStyler: function (index, row) {
                    //MatertalOutType-1	直接出库单
                    //MatertalOutType-2	转库出库单
                    //MatertalOutType-3	红冲出库单
                    //MatertalOutType-4	盘盈出库单
                    //MatertalOutType-5	盘亏出库单
                    //MatertalOutType-6	报废出库单
                    if (row.DeliveryType == "MatertalOutType-3") {
                        return 'color:red;';
                    } else if (row.DeliveryType == "MatertalOutType-2") {
                        return 'color:blue;';
                    } else if (row.DeliveryType == "MatertalOutType-4") {
                        return 'background-color:red;color:white';
                    } else if (row.DeliveryType == "MatertalOutType-5") {
                        return 'background-color:yellowgreen;color:white';
                    } else if (row.DeliveryType == "MatertalOutType-6") {
                        return ' background-color:gray; color:white';
                    }
                },  //2016-4-11 新增出库审批功能
                toolbar: [
                    {
                        id: 'btnPostCheck',
                        text: '已勾选[0]行,点击入账',
                        iconCls: 'icon-custom-View',
                        handler: function () {
                            var selected = $('#gridDeliveryList').datagrid('getSelected');
                            if (selected) {

                                $("#SaveMonth").dialog("open");
                                $("#DeliveryCheckMonth").numberbox("setValue", '@ViewData["reMonth"]');//将I_BalanceList表中的最大月份+1赋给numberbox
                                //initCheckDetailGrid(selected.EntryCode, selected.EntryType);
                                //$('#CheckEntryDetailList').dialog('open');
                            } else {
                                $.messager.alert('提示', '请选择数据行', 'info', function () {
                                });
                            }
                        }
                    }
                ],  //2016-4-11 新增出库审批功能  将选中的row注入到数组 Materials 中
                onCheck: function (index, row) {
                    if (Materials.length == 0) {
                        Materials.push(row);
                        changeRevisitViewNum();
                        return;
                    }
                    var boolContain = false;
                    $.each(Materials, function (i, info) {
                        if (info.DeliveryDetailCode == row.DeliveryDetailCode) {
                            boolContain = true;
                        }
                    });
                    if (!boolContain) {
                        Materials.push(row);
                    }
                    changeRevisitViewNum();

                },
                onUncheck: function (index, row) {
                    var obj;
                    $.each(Materials, function (i, info) {
                        if (info.DeliveryDetailCode == row.DeliveryDetailCode) {
                            obj = info;
                        }
                    });
                    Materials.remove(obj);
                    changeRevisitViewNum();
                },
                onCheckAll: function (rows) {
                    $.each(rows, function (i, infos) {

                        var hasmates = false;
                        $.each(Materials, function (index, mates) {
                            if (infos.DeliveryDetailCode == mates.DeliveryDetailCode) {
                                hasmates = true;
                            }
                        });
                        if (!hasmates)
                        { Materials.push(infos); }
                    }
                    );
                    changeRevisitViewNum();
                },
                onUncheckAll: function (rows) {
                    var obj;
                    $.each(rows, function (i, infos) {
                        $.each(Materials, function (index, mates) {
                            if (infos.DeliveryDetailCode == mates.DeliveryDetailCode) {
                                obj = infos;
                            }
                        });
                        Materials.remove(obj);

                        changeRevisitViewNum();
                    });
                },
                onLoadSuccess: function (data) {
                    $.messager.show({
                        title: '系统提示',
                        msg: '   成功加载出 ' + data.total + ' 条信息！'
                    });

                    setViewNumCheck();
                }
            });  //datagrid end

            $(".datagrid-header-check").attr("disabled", "disabled");

        } //bindGrid end

        //自动勾选已经存在的内容
        function setViewNumCheck() {
            var rows = $('#gridDeliveryList').datagrid('getRows');
            $.each(rows, function (index, item) {
                $.each(Materials, function (i, info) {
                    if (item.DeliveryDetailCode == info.DeliveryDetailCode) {
                        $('#gridDeliveryList').datagrid('checkRow', index);
                    }
                });
            });
        }


        //改变显示数量
        function changeRevisitViewNum() {
            if (Materials.length == 0) {
                $("#btnPostCheck").linkbutton({
                    text: "已勾选[" + Materials.length + "]行,点击入账",
                    iconCls: "icon-custom-cGoods"
                });
            } else if (Materials.length > 0) {
                $("#btnPostCheck").linkbutton({
                    text: "已勾选[<strong><span style='color:red'>" + Materials.length + "</span></strong>]行,点击入账",
                    iconCls: "icon-custom-cGoodsFull"
                });
            }
        }


        //提交审核结果  2016-4-11
        function submitCheckDetail() {
         
            var rem = $('#DeliveryCheckMonth').numberbox('getValue');

            if (rem < '@ViewData["reMonth"]') {
                $.messager.alert('提示', '该月已入账,请重新确认后填写', 'info', function () { })
                $("#DeliveryCheckMonth").numberbox("setValue", rem);
            }
            else {
                var CheckMonth = $("#DeliveryCheckMonth").numberbox("getValue");
                if (CheckMonth.length == 6) {

                    var jsData = new Array();
                    var i = 0;
                    //var rows = $('#RevisitTaskList').datagrid('getRows');

                    $(Materials).each(function () {
                        var obj = new Object();
                        //obj.ID = rows[i].ID;
                        obj.DeliveryDetailCode = Materials[i].DeliveryDetailCode;
                        jsData.push(obj);
                        i++;
                    });

                    $.ajax({
                        type: 'POST',
                        url: '@Url.Content("~/IM/I_Delivery/SavaPickCheck")',
                        data:
                            {
                                CheckMonth: $('#DeliveryCheckMonth').numberbox('getValue'),
                                sDeliveryDetailInfo: JSON.stringify(jsData)
                            },
                        dataType: "json",
                        success: function (jsData) {
                            $.handleJsonResult(jsData, function () {
                                Materials.length = 0;

                                $('#SaveMonth').dialog('close');
                                // setViewNumCheck();

                                $('#gridDeliveryList').datagrid('reload');

                            }

                            );
                        }
                    });
                } else {
                    $.showMsg(" '请正确填写6位数字格式，例：201601'", "提示");
                }
            }
        }


        function initSearch() {
            var urlstr = '@Url.Content("~/IM/AllDictionary/GetMaterialTypeByUserInfo")';
            $.EUIcomboboxTree("#MaterialType", {
                url: urlstr,//ajax后台取数据路径，返回的是json格式的数据
                OneOption: [{
                    id: "",
                    text: "--请选择--",
                }],
                datainfo: {},
                onSelect: function (rec) {
                    if (rec.id == "") {
                        $("#MaterialID").val("");
                        $("#MaterialTypeInfo").val("");
                        return;
                    }
                    var urlstr3 = '@Url.Content("~/IM/AllDictionary/GetMaterial?mType=")' + rec.id;
                    $("#MaterialTypeInfo").val(rec.id);
                    $("#Material").combogrid({
                        prompt: '--请选择--',
                        panelWidth: 320,
                        idField: 'ID',
                        textField: 'Name',
                        groupField: 'QRCode',
                        columns: [[
                            { field: 'MCode', title: '代码', width: 50, align: 'right' },
                            { field: 'Name', title: '名称', width: 100 },
                            { field: 'UnitName', title: '单位', width: 50 },
                            { field: 'Specification', title: '规格', width: 50, align: 'right' },
                            { field: 'RealPrice', title: '售价', width: 50 },
                            { field: 'Unit', title: '单位', width: 50, sortable: false, hidden: true }]],
                        fitColumns: true,
                        filter: function (inputText, row) {
                            var opts = $(this).combogrid('options');
                            if (row[opts.groupField] == inputText) {
                                return true;
                            } else if (row[opts.textField] == inputText) {
                                return true;
                            } else {
                                if (boolSearch) {
                                    boolSearch = false;
                                    searchBindMeterialbyName(urlstr3, inputText);
                                }
                            }
                        },
                        onChange: function (n, o) {
                            if (n == "") {
                                searchBindMeterialbyName(urlstr3, n);
                                $("#MaterialID").val("");
                            }
                        },
                        onSelect: function (rec) {
                            var g = $("#Material").combogrid("grid");	// 获取数据表格对象
                            var r = g.datagrid('getSelected');	// 获取选择的行
                            $("#MaterialID").val($('#Material').combobox('getValue'));
                        }
                    });
                    searchBindMeterialbyName(urlstr3, "");
                }
            });
        }
        function searchBindMeterialbyName(urinfo, keyWord) {
            $.ajax({
                type: 'POST',
                url: urinfo,
                dataType: "json",
                success: function (jsData) {
                    if (keyWord == "") {
                        $("#Material").combogrid("grid").datagrid("loadData", jsData);
                    } else {
                        for (var i = 0; i < jsData.length; i++) {
                            if (jsData[i].Name.indexOf(keyWord) < 0) {
                                if (jsData[i].MCode.indexOf(keyWord) < 0) {
                                    if (jsData[i].PinYin.indexOf(keyWord) < 0) {
                                        jsData.remove(jsData[i]);
                                        i--;
                                    }
                                }
                            }
                        }
                        $("#Material").combogrid("grid").datagrid("loadData", jsData);
                    }
                    boolSearch = true;
                }
            });
        }

        
        //2018-12-11 zch
        function openPrintURL() {
            //string StartDate, string EndDate, string DeliveryCode, string SelectStorage, string TargetStorage, string DeliveryType, string MaterialType, string MaterialID, bool IFCheck)
            var StartDate = $('#StartDate').datebox('getValue');
           var EndDate= $('#EndDate').datebox('getValue');
           var DeliveryCode= $('#DeliveryCode').textbox('getValue');
           var  SelectStorage=$('#SelectStorage').combobox('getValue');
          var  TargetStorage=$('#TStorageList').val();
          var  DeliveryType= $('#DeliveryType').combobox('getValue');
          var  MaterialType= $("#MaterialTypeInfo").val();
          var  MaterialID= $("#MaterialID").val();
          var IFCheck = $('#txtIFCheck').combobox('getValue');

          var url = '@Url.Content("~/BB/LS/LS_DeliveryListPrint/?StartDate=")' + StartDate + '&EndDate=' + EndDate + '&DeliveryCode=' + DeliveryCode + '&SelectStorage=' + SelectStorage +
              '&TargetStorage=' + TargetStorage + '&DeliveryType=' + DeliveryType + '&MaterialType=' + MaterialType + '&MaterialID=' + MaterialID + '&IFCheck=' + IFCheck;
          window.open(url);
        }

    </script>
}
<div id="TableForm" class="easyui-layout" style="width:auto; height: auto; margin-left: auto; margin-right: auto">
    <div data-options="region:'center',title:'出库单列表清单',iconCls:'icon-custom-cChuKuDan'">
        <div id="SearchBar" data-options="region:'north'" style="overflow: hidden; padding: 10px">
            <table>
                <tr>
                    <td>查询时间：</td>
                    <td colspan="3">
                        <input id="StartDate" class="easyui-datetimebox" style="width:150px" data-options="required:true" />
                        &nbsp;&nbsp;至&nbsp;
                        <input id="EndDate" class="easyui-datetimebox" style="width:150px" data-options="required:true" />
                    </td>
                    <td>源仓库：</td>
                    <td>
                        <input id="SelectStorage" class="easyui-combobox" style="width:150px" />
                    </td>
                    <td>目标仓库：</td>
                    <td>
                        <input id="TargetStorage" class="easyui-combotree" style="width:150px" />
                        <input id="TStorageList" type="hidden" />
                    </td>
                    <td>是否入账:</td>
                    <td>
                        <input id="txtIFCheck" class="easyui-combobox" style="width:100px" />
                    </td>
                    <td>

                </tr>
                <tr>
                    <td>分类：</td>
                    <td>
                        <input id="MaterialType" class="easyui-combobox" style="width:150px" />
                        <input id="MaterialTypeInfo" type="hidden" />
                    </td>
                    <td>物资：</td>
                    <td>
                        <input id="Material" class="easyui-combogrid" style="width:150px" />
                        <input id="MaterialID" type="hidden" />
                    </td>
                    <td>出库单号：</td>
                    <td align="left">
                        <input id="DeliveryCode" class="easyui-textbox" type="text" style="width:150px" />
                    </td>
                    <td>出库单类型：</td>
                    <td>
                        <input id="DeliveryType" class="easyui-combobox" style="width:150px" />
                    </td>
                    <td></td>
                    <td>
                        &nbsp;&nbsp;
                        <a href="javascript:void(0)" id="btnsearch" class="easyui-linkbutton" data-options="iconCls:'icon-search'" onclick="reSearch()">查询</a>
                    </td>
                    <td align="right"><a href="javascript:void(0)" id="btnprint" class="easyui-linkbutton" data-options="iconCls:'icon-print'" onclick="openPrintURL()">打印</a></td>
                   
                </tr>
                <tr>
                    <td colspan="9">
                        注：
                        @*<span style="color:red">红冲出库</span> &nbsp;|&nbsp;*@
                        <span style="color: blue">转库出库</span> &nbsp;|&nbsp;
                        <span style="background-color: red; color: white; ">盘盈出库</span>&nbsp;|&nbsp;
                        <span style="background-color: yellowgreen; color: white">盘亏出库</span> &nbsp;|&nbsp;
                        <span style="background-color: gray; color: white; ">报废出库</span>
                    </td>
                    
                </tr>
            </table>
        </div>
        <div data-options="region:'center'" style="background: #fafafa; overflow: hidden">
            <table id="gridDeliveryList"></table>
        </div>
    </div>
</div>

<div id="SaveMonth" class="easyui-dialog" title="出库入账归属月份" data-options="modal:true,collapsible:false,minimizable:false,maximizable:false,closed:true,resizable:false,iconCls:'icon-custom-cGoodsFull'" style="overflow: hidden; padding: 10px;height:170px;width:200px;">
    <table>
        <tr style="height:35px ">
            <td>
                请填写入账归属月份
            </td>
        </tr>
        <tr style="height:30px ">
            <td>
                <input id="DeliveryCheckMonth" class="easyui-numberbox" type="text" style="width:120px" data-options="required:true,prompt:'例：201601'" />
            </td>
        </tr>
        <tr style="height:35px ">
            <td>
                <a href="javascript:void(0)" id="btnSumbit" style="width:80px" class="easyui-linkbutton" data-options="iconCls:'icon-save'" onclick="submitCheckDetail()">提交</a>
            </td>
        </tr>
    </table>

</div>

